﻿//-------------------------------------------------------------------
//版权所有：版权所有(C) 2010， YIBO(China) Studio 
//系统名称：验证控件
//文件名称：InputStringClientValidator.cs
//模块名称：验证控件
//模块编号：
//作　　者：LHC
//完成日期：2010/8/29 14:34:00
//功能说明：
//-----------------------------------------------------------------
//修改记录：
//修改人：  
//修改时间：
//修改内容：
//-------------------------------------------------------------------
#region 程序集 YIBO.Validator, v1.0.100829

using System.Web.UI;
using System.Web.UI.WebControls;

#endregion
namespace MvcMall.Framework.FormValidator
{

    /// <summary>
    /// 脚本容器
    /// </summary>
    public class ValidatorContainer : WebControl
    {
        /// <summary>
        /// 是否使用UpdatePanel
        /// </summary>
        private bool isScriptManagerInside;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ValidatorContainer()
        {
            ///清楚脚本控件
            this.Controls.Clear();
        }

        /// <summary>
        /// 注册验证控件
        /// </summary>
        /// <param name="control">验证控件对象</param>
        public void AddValidatorControl(ValidateRenderControl control)
        {
            if (control != null)
            {
                this.Controls.Add(control);
            }
        }

        /// <summary>
        /// 重写输出方式
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.HasControls())
            {
                this.RenderBeginTag(writer);
                for (int i = 0; i < this.Controls.Count; i++)
                {
                    this.Controls[i].RenderControl(writer);
                    writer.WriteLine();
                }
                this.RenderEndTag(writer);
            }
        }

        /// <summary>
        /// 输出脚本头
        /// </summary>
        /// <param name="writer">HTML输出流</param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {  
            writer.WriteLine("<script type=\"text/javascript\" language=\"javascript\">");
            if (this.IsScriptManagerInside)
            {
                writer.WriteLine("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InitValidators);");
            }
            writer.WriteLine("function InitValidators()");
            writer.WriteLine("{");
        }

        /// <summary>
        /// 输出脚步头
        /// </summary>
        /// <param name="writer">HTML输出流</param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.WriteLine("}");
            writer.WriteLine("$(document).ready(function(){ InitValidators(); });");
            writer.WriteLine("</script>");
        }

        /// <summary>
        /// 是否使用UpdatePanel
        /// </summary>
        public bool IsScriptManagerInside
        {
            get
            {
                return this.isScriptManagerInside;
            }
            set
            {
                this.isScriptManagerInside = value;
            }
        }
    }
}


